package com.cy.pj.sys.service.impl;

import com.cy.pj.common.exception.ServiceException;
import com.cy.pj.common.pojo.PageObject;
import com.cy.pj.common.vo.Node;
import com.cy.pj.sys.dao.SysUserDao;
import com.cy.pj.sys.dao.SysUserRoleDao;
import com.cy.pj.sys.entity.SysUser;
import com.cy.pj.sys.service.SysUserService;
import com.cy.pj.sys.vo.SysUserDeptVo;
import io.micrometer.core.instrument.util.StringUtils;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class SysUserSeviceImpl implements SysUserService {

    @Autowired
    private SysUserDao sysUserDao;
    @Autowired
    private SysUserRoleDao sysUserRoleDao;

    @Override
    public PageObject<SysUserDeptVo> findPageObjects(String username, Integer pageCurrent) {
        //1.对参数进行校验
        if(pageCurrent==null||pageCurrent<1)
            throw new IllegalArgumentException("当前页码值无效");
        //2.查询总记录数并进行校验
        int rowCOunt= sysUserDao.getRowCount(username);
        if(rowCOunt==0)
        throw new ServiceException("没有找到对应的记录");
        //3.查询当前页记录
        int pageSize = 8;
        int startIndex=(pageCurrent-1)*pageSize;
        List<SysUserDeptVo> records = sysUserDao.findPageObjects
                (username,startIndex , pageSize);
        return new PageObject<>(rowCOunt,records,pageSize,pageCurrent);

    }

    @Override
    public int validById(Integer id, Integer valid) {
        //1.合法性验证
        if(id==null||id<=0)
            throw new ServiceException("参数不合法，id="+id);
        if(valid==null||(valid!=1&&valid!=0))
            throw new ServiceException("参数不合法，valid="+valid);
        //2.执行禁用或启用操作(admin 为后续登陆用户）
        int rows=sysUserDao.validaById(id, valid, "admin");
        if(rows==0)
            throw new ServiceException("此记录可能已经不存在");
        return rows;
    }

    @Override
    public int saveObject(SysUser entity, Integer[] roleIds) {
        //1.参数校验
        if(entity==null)
            throw new IllegalArgumentException("保存对象不能为空");
        if(StringUtils.isEmpty(entity.getUsername()))
            throw new IllegalArgumentException("用户名不能为空");
        if(StringUtils.isEmpty(entity.getPassword()))
            throw new IllegalArgumentException("密码不能为空");
        if(roleIds==null || roleIds.length==0)
            throw new IllegalArgumentException("至少要为用户分配角色");
        //2.保存用户自身信息
        //2.1 对密码进行加密
        String source=entity.getPassword();
        String salt= UUID.randomUUID().toString();
        SimpleHash sh=new SimpleHash(//Shiro 框架
                "MD5",//algorithmName 算法
                source,//原密码
                salt, //盐值
                1);//hashIterations 表示加密次数
        entity.setSalt(salt);
        entity.setPassword(sh.toHex());
        int rows=sysUserDao.insertObject(entity);
        //3.保存用户角色关系数据
        sysUserRoleDao.insertObjects(entity.getId(), roleIds);
        //4.返回结果
        return rows;
    }

    @Override
    public List<Node> findZtreeUserNodes() {
        return sysUserDao.findZtreeUserNodes();
    }

}
